﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using OF.Component.BLL;
using OF.Component.Model;
using OF.Utility;
using User.Api.Common;
using User.Api.Models;

namespace User.Api.Controllers
{
    /// <summary>
    /// 行驶证接口
    /// </summary>
    public class VehicleLicenseController : BaseApiController
    {

        /// <summary>
        /// 行驶证添加接口
        /// </summary>
        /// <param name="reqInfo">请求参数</param>
        /// <returns></returns>
        /// <exception cref="BizException"></exception>
        public ResultData<ResponseCommon<int>> Add(ReqVehicleLicenseAdd reqInfo)
        {
            var result = ResultData<ResponseCommon<int>>.Error();
            if (reqInfo.UserID <= 0)
            {
                throw new BizException(StatusCodeEnum.VehicleLicenseUserIDNotNull);
            }
            
            var oInfo = new VehicleLicenseInfo();
            reqInfo.MergeTo(oInfo);
            oInfo.CreatedDate = DateTime.Now;
            oInfo.UpdatedDate = DateTime.Now;
            oInfo.IsActive = 1;
            int nInsertID = VehicleLicenseOperate.Insert(oInfo);
            result = ResultData<ResponseCommon<int>>.Success(new ResponseCommon<int>(nInsertID));
            return result;
        }

        /// <summary>
        /// 行驶证更新信息接口
        /// </summary>
        /// <param name="reqInfo">请求参数</param>
        /// <returns></returns>
        /// <exception cref="BizException"></exception>
        public ResultData Update(ReqVehicleLicenseUpdate reqInfo)
        {
            var result = ResultData.Error();
            if (reqInfo.UserID <= 0)
            {
                throw new BizException(StatusCodeEnum.VehicleLicenseUserIDNotNull);
            }
            
            var oExist = VehicleLicenseOperate.GetByID(reqInfo.VehicleLicenseID);
            if (oExist == null)
            {
                throw new BizException(StatusCodeEnum.VehicleLicenseNotExist);
            }
            reqInfo.MergeTo(oExist);
            oExist.UpdatedDate = DateTime.Now;
            VehicleLicenseOperate.Update(oExist);
            result = ResultData.Success();
            return result;
        }

        /// <summary>
        /// 行驶证信息获取接口
        /// </summary>
        /// <param name="vehicleLicenseID"></param>
        /// <returns></returns>
        /// <exception cref="BizException"></exception>
        public ResultData<ResVehicleLicenseGet> Get(int vehicleLicenseID)
        {
            var result = ResultData<ResVehicleLicenseGet>.Error();
            var oInfo = VehicleLicenseOperate.GetByID(vehicleLicenseID);
            if (oInfo != null)
            {
                var oRes = new ResVehicleLicenseGet();
                oInfo.MergeTo(oRes);
                result = ResultData<ResVehicleLicenseGet>.Success(oRes);
            }
            else
            {
                throw new BizException(StatusCodeEnum.VehicleLicenseNotExist);
            }
            return result;
        }
    }
}